package com.zjrb.netty.config;


import com.zjrb.netty.http.NettyServer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

@Configuration
public class NettyConfig {


    private static final Logger logger = LoggerFactory.getLogger(NettyConfig.class);


    @PostConstruct
    public void initServer() {
        ThreadPoolExecutor poolExecutor = new ThreadPoolExecutor(1, 1,
                0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<>(), r -> {
            Thread t = new Thread(r, "netty-server");
            if (t.isDaemon()) {
                t.setDaemon(false);
            }
            if (t.getPriority() != Thread.NORM_PRIORITY) {
                t.setPriority(Thread.NORM_PRIORITY);
            }
            return t;
        });
        poolExecutor.execute(() -> {
            NettyServer server = new NettyServer();
            server.start();
            logger.info("netty service starting...");

        });
    }

}
